{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 引用Nuget包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><div></div><div></div><div><strong>Installed Packages</strong><ul><li><span>Microsoft.Extensions.Caching.Memory, 6.0.0</span></li><li><span>Microsoft.Extensions.Configuration.Json, 6.0.0</span></li><li><span>Senparc.Weixin.Cache.Redis, 2.15.13</span></li><li><span>Senparc.Weixin.MP, 16.19.3</span></li><li><span>Senparc.Weixin.WxOpen, 3.16.3</span></li></ul></div></div>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#r \"nuget:Senparc.Weixin.WxOpen\"\n",
    "#r \"nuget:Senparc.Weixin.Cache.Redis\"\n",
    "#r \"nuget:Senparc.Weixin.MP\"\n",
    "#r \"nuget:Microsoft.Extensions.Caching.Memory,6.0.0\"\n",
    "#r \"nuget:Microsoft.Extensions.Configuration.Json,6.0.0\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 引用命名空间"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "using Senparc.CO2NET;\n",
    "using Senparc.CO2NET.RegisterServices;\n",
    "using Senparc.Weixin.Cache.Redis;\n",
    "using Senparc.Weixin.Entities;\n",
    "using Senparc.Weixin;\n",
    "using Senparc.Weixin.MP;\n",
    "using Senparc.Weixin.WxOpen;\n",
    "using Microsoft.Extensions.DependencyInjection;\n",
    "using Microsoft.Extensions.Configuration;\n",
    "using Senparc.Weixin.WxOpen.AdvancedAPIs.Template;\n",
    "using Senparc.Weixin.MP.AdvancedAPIs.TemplateMessage;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 配置文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "var configBuilder = new ConfigurationBuilder();\n",
    "// 此处需要使用配置文件的绝对路径\n",
    "configBuilder.AddJsonFile(@\"D:\\PythonProjects\\senparc.json\", false, false);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 注册盛派SDK"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "// 关闭盛派APM\n",
    "Senparc.CO2NET.APM.Config.EnableAPM = false;\n",
    "// 不抛出异常\n",
    "Senparc.Weixin.Config.ThrownWhenJsonResultFaild = false;\n",
    "\n",
    "var config = configBuilder.Build();\n",
    "\n",
    "var senparcSetting = new SenparcSetting();\n",
    "var senparcWeixinSetting = new SenparcWeixinSetting();\n",
    "\n",
    "config.GetSection(\"SenparcSetting\").Bind(senparcSetting);\n",
    "config.GetSection(\"SenparcWeixinSetting\").Bind(senparcWeixinSetting);\n",
    "\n",
    "var services = new ServiceCollection();\n",
    "services.AddSenparcGlobalServices(config);//Senparc.CO2NET 全局注册\n",
    "\n",
    "IRegisterService register = RegisterService.Start(senparcSetting)\n",
    "                                            .UseSenparcGlobal();\n",
    "\n",
    "var redisConfigurationStr = senparcSetting.Cache_Redis_Configuration;\n",
    "Senparc.CO2NET.Cache.Redis.Register.SetConfigurationOption(redisConfigurationStr);\n",
    "//键值对缓存策略（推荐）\n",
    "Senparc.CO2NET.Cache.Redis.Register.UseKeyValueRedisNow();\n",
    "\n",
    "register.UseSenparcWeixinCacheRedis();\n",
    "\n",
    "register.UseSenparcWeixin(senparcWeixinSetting)\n",
    "        .RegisterMpAccount(senparcWeixinSetting, \"梧州珍宝巴士\")\n",
    "        .RegisterWxOpenAccount(senparcWeixinSetting, \"梧州公交\");"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 发送统一服务消息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<details open=\"open\" class=\"dni-treeview\"><summary><span class=\"dni-code-hint\"><code>WxJsonResult：{errcode:&#39;45109&#39;,errcode_name:&#39;45109&#39;,errmsg:&#39;This API has been unsupported. For more details, please view https://developers.weixin.qq.com/community/develop/doc/000ae8d6348af08e7030bc2546bc01?blockType=1 rid: 650b8665-65fe50fc-076f7520&#39;}</code></span></summary><div><table><thead><tr></tr></thead><tbody><tr><td>errcode</td><td><span>45109</span></td></tr><tr><td>ErrorCodeValue</td><td><div class=\"dni-plaintext\"><pre>45109</pre></div></td></tr><tr><td>errmsg</td><td><div class=\"dni-plaintext\"><pre>This API has been unsupported. For more details, please view https://developers.weixin.qq.com/community/develop/doc/000ae8d6348af08e7030bc2546bc01?blockType=1 rid: 650b8665-65fe50fc-076f7520</pre></div></td></tr><tr><td>P2PData</td><td><div class=\"dni-plaintext\"><pre>&lt;null&gt;</pre></div></td></tr></tbody></table></div></details><style>\r\n",
       ".dni-code-hint {\r\n",
       "    font-style: italic;\r\n",
       "    overflow: hidden;\r\n",
       "    white-space: nowrap;\r\n",
       "}\r\n",
       ".dni-treeview {\r\n",
       "    white-space: nowrap;\r\n",
       "}\r\n",
       ".dni-treeview td {\r\n",
       "    vertical-align: top;\r\n",
       "    text-align: start;\r\n",
       "}\r\n",
       "details.dni-treeview {\r\n",
       "    padding-left: 1em;\r\n",
       "}\r\n",
       "table td {\r\n",
       "    text-align: start;\r\n",
       "}\r\n",
       "table tr { \r\n",
       "    vertical-align: top; \r\n",
       "    margin: 0em 0px;\r\n",
       "}\r\n",
       "table tr td pre \r\n",
       "{ \r\n",
       "    vertical-align: top !important; \r\n",
       "    margin: 0em 0px !important;\r\n",
       "} \r\n",
       "table th {\r\n",
       "    text-align: start;\r\n",
       "}\r\n",
       "</style>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "public static readonly string WxOpenAppId = Senparc.Weixin.Config.SenparcWeixinSetting.WxOpenAppId;// 与微信小程序后台的AppId设置保持一致，区分大小写。\n",
    "public static readonly string WxOpenAppSecret = Senparc.Weixin.Config.SenparcWeixinSetting.WxOpenAppSecret;// 与微信小程序账号后台的AppId设置保持一致，区分大小写。\n",
    "public static readonly string WeixinAppId = Senparc.Weixin.Config.SenparcWeixinSetting.WeixinAppId;// 微信公众号AppId\n",
    "public static readonly string WeixinAppSecret = Senparc.Weixin.Config.SenparcWeixinSetting.WeixinAppSecret;// 微信公众号AppId\n",
    "private const string TemplateId = \"OJ-cY-IJhkw8SF_l0OZK_Cy5cKGN-uqhMiSXLxCaON8\";// 模板Id\n",
    "const string openid = \"oMr9M5SoFzyGqjHm2pvUImj1w0BQ\";// 接收测试消息的openid\n",
    "\n",
    "UniformSendData sendData = new(openid,\n",
    "                    new Mp_Template_Msg(WeixinAppId, TemplateId, \"https://mp.weixin.qq.com/s/ObnyNhpOIjij7zgS1RIvaA\",\n",
    "                    new Miniprogram_Page(WxOpenAppId, string.Empty),\n",
    "                    new\n",
    "                    {\n",
    "                        keyword1 = new TemplateDataItem(\"3075309865\"),\n",
    "                        keyword2 = new TemplateDataItem(\"2023-09-10 17:02:22\"),\n",
    "                        keyword3 = new TemplateDataItem(\"49路\"),\n",
    "                        keyword4 = new TemplateDataItem(\"010678\"),\n",
    "                        keyword5 = new TemplateDataItem(\"2.00\")\n",
    "                    }));\n",
    "\n",
    "var result = TemplateApi.UniformSend(WxOpenAppId, sendData);\n",
    "// 查看返回结果\n",
    "result.Display();"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 发送模板消息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<details open=\"open\" class=\"dni-treeview\"><summary><span class=\"dni-code-hint\"><code>WxJsonResult：{errcode:&#39;40003&#39;,errcode_name:&#39;不合法的OpenID&#39;,errmsg:&#39;invalid openid rid: 650b8666-127fe826-7727fb1e&#39;}</code></span></summary><div><table><thead><tr></tr></thead><tbody><tr><td>msgid</td><td><div class=\"dni-plaintext\"><pre>0</pre></div></td></tr><tr><td>errcode</td><td><span>不合法的OpenID</span></td></tr><tr><td>ErrorCodeValue</td><td><div class=\"dni-plaintext\"><pre>40003</pre></div></td></tr><tr><td>errmsg</td><td><div class=\"dni-plaintext\"><pre>invalid openid rid: 650b8666-127fe826-7727fb1e</pre></div></td></tr><tr><td>P2PData</td><td><div class=\"dni-plaintext\"><pre>&lt;null&gt;</pre></div></td></tr></tbody></table></div></details><style>\r\n",
       ".dni-code-hint {\r\n",
       "    font-style: italic;\r\n",
       "    overflow: hidden;\r\n",
       "    white-space: nowrap;\r\n",
       "}\r\n",
       ".dni-treeview {\r\n",
       "    white-space: nowrap;\r\n",
       "}\r\n",
       ".dni-treeview td {\r\n",
       "    vertical-align: top;\r\n",
       "    text-align: start;\r\n",
       "}\r\n",
       "details.dni-treeview {\r\n",
       "    padding-left: 1em;\r\n",
       "}\r\n",
       "table td {\r\n",
       "    text-align: start;\r\n",
       "}\r\n",
       "table tr { \r\n",
       "    vertical-align: top; \r\n",
       "    margin: 0em 0px;\r\n",
       "}\r\n",
       "table tr td pre \r\n",
       "{ \r\n",
       "    vertical-align: top !important; \r\n",
       "    margin: 0em 0px !important;\r\n",
       "} \r\n",
       "table th {\r\n",
       "    text-align: start;\r\n",
       "}\r\n",
       "</style>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "var result = Senparc.Weixin.MP.AdvancedAPIs.TemplateApi.SendTemplateMessage(WeixinAppId, openid, TemplateId,\n",
    "                \"https://mp.weixin.qq.com/s/ObnyNhpOIjij7zgS1RIvaA\",\n",
    "                new\n",
    "                {\n",
    "                    keyword1 = new TemplateDataItem(\"3075309865\"),\n",
    "                    keyword2 = new TemplateDataItem(\"2023-09-10 17:02:22\"),\n",
    "                    keyword3 = new TemplateDataItem(\"49路\"),\n",
    "                    keyword4 = new TemplateDataItem(\"010678\"),\n",
    "                    keyword5 = new TemplateDataItem(\"2.00\")\n",
    "                });\n",
    "result.Display();"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".NET (C#)",
   "language": "C#",
   "name": ".net-csharp"
  },
  "language_info": {
   "name": "python"
  },
  "orig_nbformat": 4,
  "polyglot_notebook": {
   "kernelInfo": {
    "defaultKernelName": "csharp",
    "items": [
     {
      "aliases": [],
      "name": "csharp"
     }
    ]
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
